package com.ice.day726.thread;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author ice2020x
 * @Date 2022-08-02 16:36
 * @Description: 两阶段终止
 */
public class TestThread12 {

    public static void main(String[] args) {
        TowPhaseTermination towPhaseTermination = new TowPhaseTermination();
        towPhaseTermination.start();
        try {
            Thread.sleep(3500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        towPhaseTermination.stop();
    }
}

@Slf4j
class TowPhaseTermination {
    private Thread monitor;

    // 启动监控线程
    public void start() {
        monitor = new Thread(()->{
            while(true){
                Thread thread = Thread.currentThread();
                if(thread.isInterrupted()){
                    log.debug("料理后事");
                    break;
                }
                try {
                    Thread.sleep(1000);
                    log.debug("执行监控记录");
                } catch (InterruptedException e) {
                    thread.interrupt();
                    e.printStackTrace();
                }
            }
        });
        monitor.start();
    }


    public void stop(){
        monitor.interrupt();
    }
}